package svc

import (
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
	"trusteva/app/rbac/model"
)

func initRBAC(db *gorm.DB) {
	initActions(db)
	initMenu(db)
	initRole(db)
	initUser(db)
}

func initActions(db *gorm.DB) {
	// 初始化功能

	// ACTION
	var actions []model.ActionConfig
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-ACTION]获取功能列表",
		Path:   "/v1/rbac/action/list",
		Method: "GET",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-ACTION]创建功能",
		Path:   "/v1/rbac/action/create",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-ACTION]更新功能",
		Path:   "/v1/rbac/action/update",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-ACTION]删除功能",
		Path:   "/v1/rbac/action/delete",
		Method: "POST",
	})

	// MENU
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-MENU]菜单树",
		Path:   "/v1/rbac/menu/tree",
		Method: "GET",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-MENU]创建菜单",
		Path:   "/v1/rbac/menu/create",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-MENU]更新菜单",
		Path:   "/v1/rbac/menu/update",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-MENU]删除菜单",
		Path:   "/v1/rbac/menu/delete",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-MENU]菜单添加功能",
		Path:   "/v1/rbac/menu/action/add",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-MENU]菜单删除功能列表",
		Path:   "/v1/rbac/menu/actions/add",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-MENU]菜单删除功能",
		Path:   "/v1/rbac/menu/action/delete",
		Method: "POST",
	})

	// ROLE
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-ROLE]角色列表",
		Path:   "/v1/rbac/role/list",
		Method: "GET",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-ROLE]创建角色",
		Path:   "/v1/rbac/role/create",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-ROLE]更新角色",
		Path:   "/v1/rbac/role/update",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-ROLE]删除角色",
		Path:   "/v1/rbac/role/delete",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-ROLE]角色功能列表",
		Path:   "/v1/rbac/role/action/list",
		Method: "GET",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-ROLE]角色功能绑定(传全量)",
		Path:   "/v1/rbac/role/action/bind",
		Method: "POST",
	})

	// USER
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-USER]用户列表",
		Path:   "/v1/rbac/user/list",
		Method: "GET",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-USER]创建用户",
		Path:   "/v1/rbac/user/create",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-USER]更新用户",
		Path:   "/v1/rbac/user/update",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-USER]删除用户",
		Path:   "/v1/rbac/user/delete",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-USER]重置用户密码",
		Path:   "/v1/rbac/user/password/set",
		Method: "POST",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-USER]角色功能绑定(传全量)",
		Path:   "/v1/rbac/user/action/bind",
		Method: "POST",
	})

	// 系统用户日志
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-USERLOG]日志查询",
		Path:   "/v1/rbac/user/log/list",
		Method: "GET",
	})
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-USERLOG]日志删除",
		Path:   "/v1/rbac/user/log/delete",
		Method: "POST",
	})

	// 系统信息
	actions = append(actions, model.ActionConfig{
		Name:   "[RBAC-SYSTEM]系统信息",
		Path:   "/v1/rbac/psutil/info",
		Method: "GET",
	})

	// 登录用户相关
	//actions = append(actions, model.ActionConfig{
	//	Name:   "[RBAC-USEREXT]用户登录",
	//	Path:   "/userext/login",
	//	Method: "POST",
	//})
	//actions = append(actions, model.ActionConfig{
	//	Name:   "[RBAC-USEREXT]用户信息和菜单树",
	//	Path:   "/userext/info",
	//	Method: "GET",
	//})

	if err := db.CreateInBatches(actions, len(actions)).Error; err != nil {
		logx.Error(err)
	}
}

func initMenu(db *gorm.DB) {
	var menus []model.MenuConfig
	menus = append(menus, model.MenuConfig{
		Name:     "功能管理",
		Typ:      0,
		ParentId: 0,
		Sequence: 1,
		Path:     "/actionset",
		Actions:  "1,2,3,4",
	})

	menus = append(menus, model.MenuConfig{
		Name:     "菜单管理",
		Typ:      0,
		ParentId: 0,
		Sequence: 2,
		Path:     "/menuset",
		Actions:  "5,6,7,8,9,10,11",
	})

	menus = append(menus, model.MenuConfig{
		Name:     "角色管理",
		Typ:      0,
		ParentId: 0,
		Sequence: 3,
		Path:     "/roleset",
		Actions:  "12,13,14,15,16,17",
	})

	menus = append(menus, model.MenuConfig{
		Name:     "用户管理",
		Typ:      0,
		ParentId: 0,
		Sequence: 4,
		Path:     "/accountset",
		Actions:  "18,19,20,21,22,23",
	})

	menus = append(menus, model.MenuConfig{
		Name:     "用户日志",
		Typ:      0,
		ParentId: 0,
		Sequence: 5,
		Path:     "/userlog",
		Actions:  "24,25",
	})

	menus = append(menus, model.MenuConfig{
		Name:     "系统监控",
		Typ:      0,
		ParentId: 0,
		Sequence: 6,
		Path:     "/systeminfo",
		Actions:  "26",
	})
	if err := db.CreateInBatches(menus, len(menus)).Error; err != nil {
		logx.Error(err)
	}
}

func initRole(db *gorm.DB) {
	var roles []model.RoleConfig
	roles = append(roles, model.RoleConfig{
		Name:   "ROOT",
		IsRoot: 1,
		Memo:   "RBAC INIT",
	})
	roles = append(roles, model.RoleConfig{
		Name:   "普通管理员",
		IsRoot: 0,
		Memo:   "",
	})

	if err := db.CreateInBatches(roles, len(roles)).Error; err != nil {
		logx.Error(err)
	}
}

func initUser(db *gorm.DB) {
	var users []model.User
	users = append(users, model.User{
		Username: "ADMIN",
		Login:    "admin",
		Password: "FGToKnyZ2zUMmLrtJpq+BvTrBs45XgArt5N5XPw95iE=",
		Salt:     "vqalf5zp",
		Memo:     "INIT",
		Roles:    "1",
		Company:  "NoCompany",
		Email:    "axengine@gmail.com",
	})

	users = append(users, model.User{
		Username: "普通用户",
		Login:    "admin1",
		Password: "FGToKnyZ2zUMmLrtJpq+BvTrBs45XgArt5N5XPw95iE=",
		Salt:     "vqalf5zp",
		Memo:     "INIT",
		Roles:    "2",
		Company:  "NoCompany",
		Email:    "axengine@gmail.com",
	})

	if err := db.CreateInBatches(users, len(users)).Error; err != nil {
		logx.Error(err)
	}
}
